package com.sz.app.customer.api.logistics;

import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.constants.MessagesCodes;
import com.sz.biz.logistics.bill.dto.BillingDeleteDto;
import com.sz.common.base.dto.CommonModel;
import com.sz.common.base.dto.ResultDto;
import com.sz.biz.logistics.bill.entity.BillingStatus;
import com.sz.biz.logistics.bill.service.BillingOrderRelService;
import com.sz.biz.logistics.bill.service.BillingService;
import com.sz.biz.logistics.bill.dto.BillingListStringCustomerDto;
import com.sz.biz.logistics.bill.dto.BillingUrlDto;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Function: 账单管理 <br>
 * Author: jifeixing <br>
 * Date: 2017-05-17 14:18:00
 */
@RestController
@RequestMapping("${api}/cust/financial/{type}")
@Api(description = " ", tags = "041、账单管理")
public class BillingController extends BaseController {

    @Autowired
    private BillingService billingService;
    @Autowired
    private BillingOrderRelService billingOrderRelService;

    /**
     * 根据账单号删除账单
     *
     * @param billingDeleteDto
     * @return
     */
    @ApiOperation(value = "根据账单号删除账单", notes = "根据账单号删除账单")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "type", value = "账单号", required = true, dataType = "string", paramType = "path"),
                    @ApiImplicitParam(name = "billingDeleteDto", value = "重试的任务编码列表", required = true, dataType = "BillingDeleteDto", paramType = "body")
            })
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    public ResultDto batchDeleteBillingByNos(@PathVariable String type, @RequestBody BillingDeleteDto billingDeleteDto) {
        billingService.batchdeleteByNos(billingDeleteDto.getBillNos(),type,billingDeleteDto.getCustomers());
        return getDeleteSuccessDto(MessagesCodes.MODULE_FINANCIAL);
    }

    /**
     * 获取账单下所有订单
     *
     * @param billNo
     * @return
     */
    @ApiOperation(value = "获取账单下所有订单", notes = "获取账单下所有订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "billNo", value = "账单号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "订单类型", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "productUid", value = "产品Uid", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "customerId", value = "账号名", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "startEffectTime", value = "起始出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endEffectTime", value = "结束出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "isSearch", value = "true, false", dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/orders", method = {RequestMethod.GET})
    public ResultDto getOrdersByBillNo(@RequestParam(required = false) String q,
                                       @RequestParam(required = true) String billNo,
                                       @PathVariable String type,
                                       @RequestParam(required = false) String orderNo,
                                       @RequestParam(required = false) String productUid,
                                       @RequestParam(required = true) Integer customerId,
                                       @RequestParam(required = false) String startEffectTime,
                                       @RequestParam(required = false) String endEffectTime,
                                       @RequestParam(required = false, defaultValue = "false") Boolean isSearch,
                                       @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                       @RequestParam(required = false, defaultValue = "10") int pageSize,
                                       @RequestParam(required = false) String sort,
                                       @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        return billingOrderRelService.getOrdersByBillNoForCustomer(q, billNo, orderNo, productUid, customerId, startEffectTime, endEffectTime, isSearch, pageIndex, pageSize, sort, asc,type);
    }

    /**
     * 所有账单状态
     *
     * @return
     */
    @ApiOperation(value = "所有账单状态", notes = "所有账单状态")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "type", value = "账单号", required = true, dataType = "string", paramType = "path")})
    @RequestMapping(value = "/billingStatus", method = RequestMethod.GET)
    public ResultDto getBillingStatus() {
        List<CommonModel> list = billingService.getBillingStatusForCustomer();
        ResultDto result = new ResultDto();
        result.setData(list);
        return result;
    }

    /**
     * 分页查询账单列表
     */
    @ApiOperation(value = "搜索账单", notes = "搜索账单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "billNo", value = "账单号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "账单类型", dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "customerId", value = "账号", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "includeAllAudit", value = "当status为AUDITING时，返回AUDITING以上的状态", dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "startEffectTime", value = "起始出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endEffectTime", value = "结束出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public ResultDto findBillingList(@RequestParam(required = false) String q,
                                     @RequestParam(required = false) String billNo,
                                     @PathVariable String type,
                                     @RequestParam(required = false) String orderNo,
                                     @RequestParam(required = true) Integer customerId,
                                     @RequestParam(required = false) BillingStatus status,
                                     @RequestParam(required = false) boolean includeAllAudit,
                                     @RequestParam(required = false) String startEffectTime,
                                     @RequestParam(required = false) String endEffectTime,
                                     @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                     @RequestParam(required = false, defaultValue = "10") int pageSize,
                                     @RequestParam(required = false) String sort,
                                     @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        return billingService.findByFilterForCustomer(q, billNo, orderNo, customerId, status, includeAllAudit, startEffectTime, endEffectTime, pageIndex, pageSize, sort, asc,type);
    }

    /**
     * 分页查询账单列表
     */
    @ApiOperation(value = "搜索账单", notes = "搜索账单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billNo", value = "账单号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "账单类型", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "customerId", value = "账号", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "startEffectTime", value = "起始出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endEffectTime", value = "结束出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query")})
    @RequestMapping(value = "/list/edi", method = {RequestMethod.GET})
    public ResultDto billsForEDI(@RequestParam(required = false) String billNo,
                                     @PathVariable String type,
                                     @RequestParam(required = false) String orderNo,
                                     @RequestParam(required = true) Integer customerId,
                                     @RequestParam(required = false) String startEffectTime,
                                     @RequestParam(required = false) String endEffectTime) {
        List<BillingListStringCustomerDto> billingListStringCustomerDtos = billingService.billsForEDI(billNo, orderNo, customerId, billingService.populateTime(startEffectTime), billingService.populateTime(endEffectTime),type);
        ResultDto resultDto = new ResultDto();
        resultDto.setData(billingListStringCustomerDtos);
        return resultDto;
    }

    /**
     * 账单导出
     */
    @ApiOperation(value = "账单导出", notes = "账单导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "账单类型", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "billNos", value = "账单号集合", dataType = "List", paramType = "body", required = true)
    })
    @RequestMapping(value = "/export", method = {RequestMethod.POST})
    public ResultDto export(@PathVariable String type,@RequestBody List<String> billNos) throws ParseException {
    	List<BillingUrlDto> list = billingService.exportCustomer(billNos,type);
        ResultDto result = new ResultDto();
        result.setData(list);
        return result;
    }

    /**
     * 根据billNo审批流程
     *
     * @param billNo
     * @return 查询到的审批流程
     */
    @RequestMapping(value = "/approval/{billNo}", method = RequestMethod.GET)
    @ApiOperation(value = "查询审批流程", notes = "根据账单号获取审批流程")
    public ResultDto findFinancialApprovals(@ApiParam(value = "billNo", required = true) @PathVariable String billNo) {
        List<Map<String, String>> approvals = billingService.findFinancialApprovals(billNo);
        ResultDto result = new ResultDto();
        result.setData(approvals);
        return result;
    }
}
